/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.services.manage.testproject.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import shop.core.common.dao.BaseDao;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.testproject.bean.Testproject;
import shop.services.manage.testproject.dao.TestprojectDao;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @类名称：TestprojectDaoImpl
 * @创建人：wzl
 * @创建时间：2018-05-24 下午15:17:35  
 * @版本号：1.0
 * @描述：
 */
@Repository("testprojectDaoManage")
public class TestprojectDaoImpl  implements TestprojectDao {
    @Resource(name = "sqlBatchSessionTemplate")
    private SqlSessionTemplate sqlBatchSessionTemplate;

    @Resource
    private BaseDao dao;

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    public PagerModel selectPageList(Testproject e) {
        return dao.selectPageList("manage.testproject.selectPageList","manage.testproject.selectPageCount", e);
    }

    public List selectList(Testproject e) {
        return dao.selectList("manage.testproject.selectList", e);
    }

    public Testproject selectOne(Testproject e) {
        return (Testproject) dao.selectOne("manage.testproject.selectOne", e);
    }

    public int delete(Testproject e) {
        return dao.delete("manage.testproject.delete", e);
    }

    public int update(Testproject e) {
        return dao.update("manage.testproject.update", e);
    }

    public int deletes(String[] ids) {
        Testproject e = new Testproject();
        for (int i = 0; i < ids.length; i++) {
            e.setId(ids[i]);
            delete(e);
        }
        return 0;
    }

    public int insert(Testproject e) {
        return dao.insert("manage.testproject.insert", e);
    }

    public int deleteById(int id) {
        return dao.delete("manage.testproject.deleteById", id);
    }

    @Override
    public Testproject selectById(String id) {
        return (Testproject) dao.selectOne("manage.testproject.selectById", id);
    }

    @Override
    public int insertList(List<Testproject> testprojectList) {
        int result = 1;
        SqlSession batchSqlSession = null;
        try {
            batchSqlSession =sqlBatchSessionTemplate
                    .getSqlSessionFactory()
                    .openSession(ExecutorType.BATCH, false);// 获取批量方式的sqlsession
            batchSqlSession.getConnection().setAutoCommit(false);
            int batchCount = 2000;// 每批commit的个数
            int batchLastIndex = batchCount;// 每批最后一个的下标
            for (int index = 0; index < testprojectList.size();) {
                if (batchLastIndex >= testprojectList.size()) {
                    batchLastIndex = testprojectList.size();
                    result = result * batchSqlSession.insert("manage.testproject.batchInsert",testprojectList.subList(index, batchLastIndex));
                    batchSqlSession.commit();
                    System.out.println("index:" + index+ " batchLastIndex:" + batchLastIndex);
                    break;// 数据插入完毕，退出循环
                } else {
                    result = result * batchSqlSession.insert("manage.testproject.batchInsert",testprojectList.subList(index, batchLastIndex));
                    batchSqlSession.commit();
                    System.out.println("index:" + index+ " batchLastIndex:" + batchLastIndex);
                    index = batchLastIndex;// 设置下一批下标
                    batchLastIndex = index + (batchCount - 1);
                }
            }
            batchSqlSession.commit();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } finally {
            batchSqlSession.close();
        }
        return result;
    }
}

